﻿namespace MefContrib.Models.Provider.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NUnit.Framework;

    [TestFixture]
    public class DefinitionProviderPartCatalogTests
    {
        [Test]
        public void CatalogShouldUseDefaultObjectFactoryWhenCreated()
        {
            DefinitionProviderPartCatalog catalog =
                new DefinitionProviderPartCatalog<NullDefinitionProvider>();

            Assert.IsTrue(catalog.Factory.GetType() == typeof(DefaultObjectFactory));
        }

        [Test]
        public void CatalogShouldCreateProviderOfCorrectTypeIfInstanceNotSpecified()
        {
            DefinitionProviderPartCatalog catalog =
                new DefinitionProviderPartCatalog<NullDefinitionProvider>();

            Assert.IsTrue(catalog.Provider.GetType() == typeof(NullDefinitionProvider));
        }

        [Test]
        public void ProviderShouldNotBeNullIfOnlyTypeIsSpecified()
        {
            DefinitionProviderPartCatalog catalog =
                new DefinitionProviderPartCatalog<NullDefinitionProvider>();

            Assert.IsNotNull(catalog.Provider);
        }

        [Test, ExpectedException(typeof(InvalidCastException))]
        public void CatalogShouldThrowExceptionIfProviderInstanceNotMatchProviderType()
        {
            DefinitionProviderPartCatalog catalog =
                new DefinitionProviderPartCatalog<NullDefinitionProvider>(new FakeDefinitionProvider());
        }

        [Test]
        public void ProviderShouldReturnSameInstanceThatWasPassedIntoConstructor()
        {
            NullDefinitionProvider provider =
                new NullDefinitionProvider();

            DefinitionProviderPartCatalog catalog =
                new DefinitionProviderPartCatalog<NullDefinitionProvider>(provider);

            Assert.AreSame(provider, catalog.Provider);
        }

        [Test, ExpectedException(typeof(ArgumentNullException))]
        public void CatalogShouldThrowExceptionIfNullReferenceIsPassedIntoConstructor()
        {
            DefinitionProviderPartCatalog catalog =
                new DefinitionProviderPartCatalog<NullDefinitionProvider>((DefinitionProvider)null);
        }

        [Test]
        public void NullParameterShouldBePassedToProvider()
        {
            DefinitionProviderPartCatalog catalog =
                new DefinitionProviderPartCatalog<FakeDefinitionProvider>(new object[] {null});

            FakeDefinitionProvider provider = 
                catalog.Provider as FakeDefinitionProvider;

            Assert.IsNull(provider.Parameter);
        }

        [Test]
        public void ParameterShouldBePassedToProvider()
        {
            DefinitionProviderPartCatalog catalog =
                new DefinitionProviderPartCatalog<FakeDefinitionProvider>("dummy value");

            FakeDefinitionProvider provider =
                catalog.Provider as FakeDefinitionProvider;

            Assert.AreEqual(provider.Parameter, "dummy value");
        }
    }

    public class NullDefinitionProvider : DefinitionProvider
    {
        public override IEnumerable<PartDescription> Parts
        {
            get { return Enumerable.Empty<PartDescription>(); }
        }
    }

    public class FakeDefinitionProvider : DefinitionProvider
    {
        public FakeDefinitionProvider()
        {
        }

        public FakeDefinitionProvider(string parameter)
        {
            this.Parameter = parameter;
        }

        public override IEnumerable<PartDescription> Parts
        {
            get { return Enumerable.Empty<PartDescription>(); }
        }

        public string Parameter { get; set; }
    }
}